const router = require('koa-router')()
const connection = require('./db'); // 导入数据库连接对象



router.post('/1', async (ctx, next) => {
  let url =ctx.url;
    let request=ctx.request;
    let reqquery = request.query;
    console.log(request.body);
    if (request.body.remember == 1) {
      let studentid = request.body.mobile;
      const query = `SELECT * FROM student WHERE studentid = ${studentid}`;
  
      try {
        const results = await new Promise((resolve, reject) => {
          connection.query(query, function (error, results, fields) {
            if (error) {
              reject(error);
            }
            resolve(results);
          });
        });
    
        // 在查询完成后再处理结果
        let list = results;
        console.log(list);
    
        // 设置 ctx.body 在查询完成后
        if (list.length > 0 && results[0].password == request.body.code) {ctx.body = {
          message: '成功得到了token',
          code: 0,
          token: 1,
          usersname:results[0].username,
          avatar:results[0].avatar
        };}
    
        // 其他后续处理可以在这里进行
    
      } catch (error) {
        // 错误处理
        console.error(error);
        // 设置错误状态码或其他响应
        ctx.status = 500; // 例如设置状态码为 500
        ctx.body = {
          message: '查询出错',
          code: 1,
        };
      }
    }
    else if (request.body.remember == 2) {
      let teacherid = request.body.mobile;
      const query = `SELECT * FROM teacher WHERE teacherid = ${teacherid}`;
    
      try {
        const results = await new Promise((resolve, reject) => {
          connection.query(query, function (error, results, fields) {
            if (error) {
              reject(error);
            }
            resolve(results);
          });
        });
    
        // 在查询完成后再处理结果
        let list = results;
        console.log(list);
    
        // 设置 ctx.body 在查询完成后
        if (list.length > 0 && results[0].password == request.body.code) {ctx.body = {
          message: '成功得到了token',
          code: 0,
          token: 1,
          usersname:results[0].username,
          avatar:results[0].avatar
        };}
    
        // 其他后续处理可以在这里进行
    
      } catch (error) {
        // 错误处理
        console.error(error);
        // 设置错误状态码或其他响应
        ctx.status = 500; // 例如设置状态码为 500
        ctx.body = {
          message: '查询出错',
          code: 1,
        };
      }
    }
    

    else if(request.body.remember==3){
      let adminid = request.body.mobile;
      const query = `SELECT * FROM admin WHERE adminid = ${adminid}`;
  
      try {
        const results = await new Promise((resolve, reject) => {
          connection.query(query, function (error, results, fields) {
            if (error) {
              reject(error);
            }
            resolve(results);
          });
        });
    
        // 在查询完成后再处理结果
        let list = results;
        console.log(list);
        console.log(results[0].adminname);
        // 设置 ctx.body 在查询完成后
        if (list.length > 0 && results[0].password == request.body.code) {ctx.body = {
          message: '成功得到了token',
          code: 0,
          token: 1,
          usersname:results[0].adminname
        };}
    
        // 其他后续处理可以在这里进行
    
      } catch (error) {
        // 错误处理
        console.error(error);
        // 设置错误状态码或其他响应
        ctx.status = 500; // 例如设置状态码为 500
        ctx.body = {
          message: '查询出错',
          code: 1,
        };
      }
    }
    /*ctx.body={
      message:'成功得到了token',
      code:0,
      token:1,
  }*/
  })

  module.exports = router